package com.smart.manager.task;


import cn.hutool.core.util.IdUtil;
import com.smart.common.annotation.TaskBean;
import com.smart.common.annotation.TaskMethod;
import com.smart.common.entities.domain.TaskResult;
import com.smart.manager.member.entities.enums.ChickenState;
import com.smart.manager.member.entities.models.MemberMaterialChicken;
import com.smart.manager.member.entities.views.MemberView;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Log4j2
@Component
@TaskBean
@RequiredArgsConstructor
public class ChickenTask {

    @TaskMethod(title = "鸡蛋生成任务")
    public synchronized TaskResult chickenTask() {
        LocalDate date = LocalDate.now();
        int senior = 90; // 设定生成概率为90%  // 高级用户
        int intermediate = 60; // 设定生成概率为60%
        int lower = 20; // 设定生成概率为80%

        try {
        String sql = "SELECT pig.member as member, pig.chicken as chicken, pig.code as code ,pig.is_full as full FROM member_material_pig AS pig WHERE pig.chicken IS NOT NULL AND pig.chicken != 0 AND pig.deleted = false";
        Random random = new Random();
        DB.sqlQuery(sql).findEach(x -> {
            Long member = x.getLong("member");
            String code = x.getString("code");
            Boolean full = x.getBoolean("full");
            List<MemberMaterialChicken> chickens = new ArrayList<>();
            String uuid = IdUtil.fastSimpleUUID().substring(0, 8).toUpperCase();
            int winning = 0;
            int winningPercentage = 0;
            for (int i = 0; i < x.getInteger("chicken"); i++) {
                if (full){
                    winningPercentage = senior;
                }else {
                    winningPercentage = intermediate;
                }
                int randomNumber = random.nextInt(100); // 生成0到99之间的随机数
                if (randomNumber < winningPercentage) {
                    winning++;
                }
            }
            //保底
            if (winning < 2) {
                winning = 2;
            }
            //生成
            for (int i = 0; i < winning; i++) {
                int i1 = random.nextInt(2);
                int withHour = 0;
                if (i1 == 1) {
                    withHour = random.nextInt(5) + 7;  //生成上午的小时数
                } else {
                    withHour = random.nextInt(18 - 12 + 1) + 12;  //生成下午的小时
                }
                int withMinute = random.nextInt(60);  //生成上午的小时数
                int withSecond = random.nextInt(60);  //生成上午的小时数
                LocalDateTime localDateTime = LocalDateTime.now().withHour(withHour).withMinute(withMinute).withSecond(withSecond);
                MemberMaterialChicken chicken = new MemberMaterialChicken()
                        .setCode(uuid).setPigCode(code)
                        .setGenerationTime(date)
                        .setDisplayTime(localDateTime)
                        .setMember(new MemberView().setId(member))
                        .setState(ChickenState.L1);
                chickens.add(chicken);
            }
            DB.insertAll(chickens);
        });
        }catch (Exception e){
            e.printStackTrace();
        }
        return new TaskResult(true, "生成成功");
    }
}
